#include <iostream>
#include <string>
#include <fstream>
#define MAX 1000
//存储的文件
#define USER_INFO "userInfo.txt"
#include <vector>

using namespace std;

//设计联系人结构体
class Person
{
public:
	string m_name;
	string m_sex;
	int m_age;
	string m_phone;
	string m_addr;
};
//设计通讯录结构体
class Addressbooks
{
public:
	//默认构造函数
	Addressbooks()
	{
		this->initVector();
	}
	void initVector()
	{
		vPerson.clear();
		ifstream ifs;
		ifs.open(USER_INFO, ios::in);
		if (!ifs.is_open())
		{
			cout << "文件读取失败" << endl;
			return;
		}
		while (ifs >> p.m_name && ifs >> p.m_sex && ifs >> p.m_age && ifs >> p.m_phone && ifs >> p.m_addr)
		{
			vPerson.push_back(p);
		}
		ifs.close();
		cout << "当前通讯录数量为" << vPerson.size() << endl;
		m_size = vPerson.size();
		
	}
	Person p;
	Person personAray[MAX];
	int m_size;
	vector<Person>vPerson;
};

void showMenu()
{
	cout << "***************" << endl;
	cout << "1.添加联系人" << endl;
	cout << "2.显示联系人" << endl;
	cout << "3.删除联系人" << endl;
	cout << "4.查找联系人" << endl;
	cout << "5.修改联系人" << endl;
	cout << "6.清空联系人" << endl;
	cout << "0.退出通讯录" << endl;
	cout << "***************" << endl;

}

//将容器中的数据重遍历
void forPerson(Addressbooks* add)
{
	//将删除后剩余的文件进行存储
	ofstream ofs;	//以写文件形式打开文件
	ofs.open(USER_INFO, ios::out | ios::trunc);
	// 遍历容器，将容器中的所有数据重新写入到文件中，做更新
	for (vector<Person>::iterator it = add->vPerson.begin(); it != add->vPerson.end(); it++)
	{
		ofs << (*it).m_name << " " << (*it).m_sex << " " << (*it).m_age << " " << (*it).m_phone << " " << (*it).m_addr << endl;
	}
	ofs.close();
	//Addressbooks::Addressbooks();    //重新获取存入的文件数量以及内容；
}

//1.添加联系人
void addPerson(Addressbooks* add)
{
	if (add->m_size == MAX)
	{
		cout << "通讯录已满，无法添加" << endl;
	}
	else
	{
		
		// 添加联系人姓名
		cout << "请输入联系人姓名" << endl;
		string name;
		cin >> name;
		add->personAray[add->m_size].m_name = name;
		// 添加联系人性别
		cout << "请输入联系人性别" << endl;
		cout << "1.男性" << endl;
		cout << "2.女性" << endl;
		string sex;

		while (true)
		{
			cin >> sex;
			// 判断通讯录中是否存在输入有误的值
			if (sex == "1" || sex == "2")
			{
				add->personAray[add->m_size].m_sex = sex;
				cout << "性别为" << add->personAray[add->m_size].m_sex << endl;
				break;
			}
			cout << "输入有误请重新输入" << endl;
		}
		// 添加联系人年龄
		int age;
		cout << "请输入联系人年龄" << endl;
		cin >> age;
		add->personAray[add->m_size].m_age = age;
		// 添加联系人电话
		string phone;
		cout << "请输入联系人电话" << endl;
		cin >> phone;
		add->personAray[add->m_size].m_phone = phone;
		// 添加联系人地址
		string address;
		cout << "请输入联系人地址" << endl;
		cin >> address;
		add->personAray[add->m_size].m_addr = address;
		cout << "添加成功" << endl;
		//更新通讯录人数
		add->m_size++;
		ofstream ofs;	//文件操作对象
		ofs.open(USER_INFO, ios::out | ios::app);   //利用追加的方式写文件
		ofs << name << " " << sex << " " << age << " " << phone << " " << address << endl;
		ofs.close();
		Addressbooks abs;    //重新获取存入的文件数量以及内容；
		abs.initVector();
		system("pause");
		system("cls");
	}
}

//2.显示联系人
void printPerson(Addressbooks add)
{
	add.initVector();
	if (add.m_size == 0)
	{
		cout << "当前记录为空" << endl;
	}
	else
	{
		for (vector<Person>::iterator it = add.vPerson.begin(); it != add.vPerson.end(); it++)
		{
			cout << "姓名 " << (*it).m_name << "\t";
			cout << "性别 " << ((*it).m_sex == "1" ? "男性" : "女性") << "\t";
			cout << "年龄 " << (*it).m_age << "\t";
			cout << "住址 " << (*it).m_addr << "\t";
			cout << "电话 " << (*it).m_phone << endl;
		}
		system("pause");
		system("cls");
	}
}

//3.删除联系人
void deletePerson(Addressbooks* add)
{
	add->initVector();
	bool find_person = false;
	if (add->m_size == 0)
	{
		cout << "当前记录为空" << endl;
	}
	else
	{
		cout << "请输入联系人姓名" << endl;
		string name;
		cin >> name;
		//遍历容器，判断容器中是否有输入的姓名
		cout << add->vPerson.size() << endl;
		for (vector<Person>::iterator it = add->vPerson.begin(); it != add->vPerson.end(); it++)
		{
			if ((*it).m_name == name)
			{
				find_person = true;
				cout << "找到了通讯录内成员信息" << endl;
				it = add->vPerson.erase(it); //将vector容器内数据删除
				if (it == add->vPerson.end()--)  //防止内存溢出
				{
					break;
				}
			}

		}
		if (find_person == true)
		{
			cout << "进入" << endl;
			add->m_size--;
			cout << "删除成功" << endl;
			forPerson(add);
		}
		else
		{
			cout << "暂未查到该成员信息" << endl;
		}
	}
}

//4.查找联系人
void findPerson(Addressbooks add)
{
	cout << "请输入您要查找的人的姓名" << endl;
	string name;
	cin >> name;
	for (vector<Person>::iterator it = add.vPerson.begin(); it != add.vPerson.end(); it++)
	{
		if ((*it).m_name == name)
		{
			cout << "查询成功" << endl;
			cout << "姓名 " << (*it).m_name << "\t";
			cout << "性别 " << ((*it).m_sex == "1" ? "男性" : "女性") << "\t";
			cout << "年龄 " << (*it).m_age << "\t";
			cout << "住址 " << (*it).m_addr << "\t";
			cout << "电话 " << (*it).m_phone << endl;
			return;
		}
	}
	cout << "查无此人" << endl;

}

//5.修改联系人
void updatePerson(Addressbooks *add)
{
	cout << "请输入您要修改的联系人姓名" << endl;
	string name;
	cin >> name;
	for (vector<Person>::iterator it = add->vPerson.begin(); it != add->vPerson.end(); it++)
	{
		if ((*it).m_name == name)
		{
			cout << "请输入要修改的姓名" << endl;
			string m_name;
			cin >> m_name;
			(*it).m_name = m_name;

			cout << "请输入要修改的年龄" << endl;
			int m_age;
			cin >> m_age;
			(*it).m_age = m_age;

			cout << "请输入要修改的性别" << endl;
			string m_sex;
			cin >> m_sex;
			(*it).m_sex = m_sex;

			cout << "请输入要修改的电话" << endl;
			string m_tel;
			cin >> m_tel;
			(*it).m_phone = m_tel;

			cout << "请输入要修改的地址" << endl;
			string m_add;
			cin >> m_add;
			(*it).m_addr = m_add;
		}
	}
	cout << "修改成功" << endl;
	forPerson(add);
}

//清空联系人
void cleanFile(Addressbooks* add)
{
	cout << "是否要清除记录" << endl;
	cout << "1.清除记录" << endl;
	cout << "2.取消" << endl;
	int num;
	while (true)
	{
		cin >> num;
		if (num == 1)
		{
			cout << "数据已经被清空" << endl;
			add->vPerson.clear();
			forPerson(add);
			system("pause");
			system("cls");
			return;
		}
		else if (num == 2)
		{
			cout << "程序退出主菜单" << endl;
			system("pause");
			system("cls");
			return;
		}
		else
		{
			cout << "输入有误，请重新输入" << endl;
		}
	}
	system("pause");
	system("cls");
}

int main()
{
	Addressbooks add;
	int select;
	//add.m_size = 0;
	while (true)
	{
		showMenu();

		cin >> select;
		switch (select)
		{
		case 0:		//退出通讯录
			cout << "通讯录退出成功" << endl;
			system("pause");
			return 0;
			break;
		case 1:		//添加联系人
			addPerson(&add);
			break;
		case 2:		//显示联系人
			printPerson(add);
			break;
		case 3:		//删除联系人
			deletePerson(&add);
			break;
		case 4:		//查找联系人
			findPerson(add);
			break;
		case 5:		//修改联系人
			updatePerson(&add);
			break;
		case 6:		//清空联系人
			cleanFile(&add);
			break;
		default:
			break;
		}
	}

	return 0;
}